package com.hzy.yiyaoba.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.hzy.yiyaoba.utils.BitmapManager;
import com.hzy.yiyaoba.utils.HttpManager;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.umeng.analytics.MobclickAgent;

public abstract class BaseFragment extends Fragment implements OnScrollListener{

	protected View contentView = null;
	protected HttpUtils mHttpUtils;
	protected BitmapUtils mBmpUtils;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mHttpUtils = HttpManager.getInstance().getHttpUtils();
		mBmpUtils = BitmapManager.getInstance(getActivity()).getBitmapUtils();
		initEnvironment();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		contentView = inflater.inflate(getLayoutRes(), container, false);
		initView();
		return contentView;
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(getClassName());
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(getClassName());
	}

	protected View findViewById(int id) {
		return contentView.findViewById(id);
	}

	protected static String getSimpleName(Class<?> c) {
		return c.getSimpleName();
	}
	
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		switch (scrollState) {
		case SCROLL_STATE_IDLE:
			mBmpUtils.resume();
			break;
		case SCROLL_STATE_TOUCH_SCROLL:
			
			break;
		case SCROLL_STATE_FLING:
			mBmpUtils.pause();
			break;
		default:
			break;
		}
	}
	
	/**
	 * 俩个view是同一个则显示，否则消失
	 * 
	 * @param view1
	 * @param view2
	 */
	protected void showView(View view1, View view2) {
		setViewVisible(view2, view1.getId() == view2.getId());
	}

	/**
	 * 设置view可见性
	 * 
	 * @param v
	 * @param isShow
	 */
	protected void setViewVisible(View v, boolean isShow) {
		v.setVisibility(isShow ? View.VISIBLE : View.INVISIBLE);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

	}

	protected abstract int getLayoutRes();

	protected abstract String getClassName();
	
	protected abstract void initEnvironment();
	
	protected abstract void initView();
}
